import socket
import json
import binascii

HOST = '127.0.0.1'
PORT = 65432

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((HOST, PORT))
    s.listen()
    print('Listening on', (HOST, PORT))
    conn, addr = s.accept()
    with conn:
        print('Connected by', addr)
        data = b''
        while True:
            chunk = conn.recv(4096)
            if not chunk:
                break
            data += chunk
        # 解析JSON数据
        student = json.loads(data.decode('utf-8'))
        print('Received JSON data:', student)
        # 将图像数据转换为二进制格式
        image_data_binary = binascii.unhexlify(student['image'])
        # 将二进制图像数据写入文件
        with open('photo.png', 'wb') as f:
            f.write(image_data_binary)
        print('Saved image file: photo.png')